package com.qwert.shortlink.project.common.convention.result;

import com.qwert.shortlink.project.common.convention.error.BaseErrorCode;
import com.qwert.shortlink.project.common.convention.error.IErrorCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2024/6/25 10:49
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Result<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 5679018624309023727L;
    private String code;
    private String message;
    private T data;
    private String requestId;

    private void setStatus(IErrorCode errorCode) {
        this.setMessage(errorCode.getMessage());
        this.setCode(errorCode.getCode());
    }

    public static <T> Result<T> create(IErrorCode errorCode, T data) {
        Result<T> result = new Result<>();
        result.setStatus(errorCode);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> create(String code, String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(T data) {
        return create("0", "操作成功", data);
    }
    public static <T> Result<T> success() {
        return create("0", "操作成功", null);
    }

    public static <T> Result<T> fail(IErrorCode errorCode,T data) {
        return create(errorCode, data);
    }
    public static <T> Result<T> fail(String errorCode, String errorMessage) {
        return create(errorCode, errorMessage,null);
    }
    public static <T> Result<T> fail(){
        return create(BaseErrorCode.CLIENT_ERROR, null);
    }

}
